/**
 * ItemDAO.java
 * @author GorillaFunch
 * @created 2012.10.12
 */
package kr.ac.kongju.gedl.board.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


import kr.ac.kongju.gedl.board.vo.BulletinBoardVO;
import kr.ac.kongju.gedl.board.vo.ContentsVO;
import kr.ac.kongju.gedl.common.util.ListResultVO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;


import egovframework.rte.psl.dataaccess.EgovAbstractDAO;

@Repository("contentsDAO")
public class BoardContentsDAO extends EgovAbstractDAO{
	private final Logger logger = LoggerFactory.getLogger(getClass());
	
	
	
	/*************************************************************************
	 * BoardContentsVO - 오류신고
	 */
	/**
	 * registerBoardContents
	 * BoardContents 등록
	 * @param contentsVO
	 * @return
	 * @throws SQLException 실패시
	 */
	public ContentsVO registerBoardContents(ContentsVO contentsVO){
		try{
			insert("Board.registerBoardContents", contentsVO);
		}catch(Exception e){
			logger.error("cause:{}", e.toString());
		}
		return contentsVO;
	}
	
	/**
	 * retrieveBoardContents
	 * BoardContents 조회
	 * @param contentsVO
	 * @return
	 * @throws SQLException
	 */
	public List<ContentsVO> retrieveBoardContents(ContentsVO contentsVO){
		
		List<ContentsVO> list = new ArrayList<ContentsVO>();
		
		try{
			list = list("Board.retrieveBoardContents", contentsVO);
		}catch(Exception e){
			logger.error("cause:{}", e.toString()); 
		}
		
		return list;
	}
	/**
	 * updateBoardContents
	 * BoardContents 갱신
	 * @param contentsVO
	 * @return
	 * @throws SQLException
	 */
	public ContentsVO updateBoardContents(ContentsVO contentsVO){
		try{
			int result = update("Board.updateBoardContents", contentsVO);
			if(result > 0){
				logger.debug("Board.updateBoardContents contentsVOId:{} title:{}", contentsVO.getContentsGroupId(), contentsVO.getContentsNm());
			}else{
				logger.debug("Board.updateBoardContents NULL");
			}
		}catch(Exception e){
			logger.error("cause:{}", e.toString());
		}
		return contentsVO;
	}
	/**
	 * registerBoardContentsAnswer
	 * BoardContents 답변정보 갱신
	 * @param contentsVO
	 * @return
	 * @throws SQLException
	 */
	public ContentsVO registerBoardContentsAnswer(ContentsVO contentsVO){
		try{
			int result = update("Board.registerBoardContentsAnswer", contentsVO);
			if(result > 0){
				logger.debug("Board.registerBoardContentsAnswer contentsVOId:{} title:{}", contentsVO.getContentsGroupId(), contentsVO.getContentsNm());
			}else{
				logger.debug("Board.registerBoardContentsAnswer NULL");
			}
		}catch(Exception e){
			logger.error("cause:{}", e.toString());
		}
		return contentsVO;
	}
	/**
	 * updateBoardContentsVisit
	 * BoardContents 조회수 갱신
	 * @param contentsVOId
	 * @return
	 * @throws SQLException
	 */
	public int updateBoardContentsVisit(int contentsVOId){
		int result = 0;
		try{	
			result = update("Board.updateBoardContentsVisit", contentsVOId);//성공:1 실패:0
			logger.debug("Board.updateBoardContentsVisit contentsVOId:{} result:{}", contentsVOId, result);
		}catch(Exception e){
			logger.error("cause:{}", e.toString());
		}
		return result;
	}
	
	/**
	 * deleteBoardContents
	 * BoardContents 삭제
	 * @param contentsVO
	 * @return success delete row count 
	 * @throws SQLException
	 */
	public int deleteBoardContents(ContentsVO contentsVO){
		int result = 0;
		try{
			result = delete("Board.deleteBoardContents", contentsVO);//삭제한 row수 반환
			if(result > 0){
				logger.debug("Board.deleteBoardContents SUCCESS:{}", result);
			}else{
				logger.debug("Board.deleteBoardContents FAIL:{}", result);
			}
		}catch(Exception e){
			logger.error("cause:{}", e.toString());
		}
		return result;
	}
	
	
	
	/**
	 * retrieveListCountBoardContents
	 * BoardContents 검색결과 수
	 * @param searchCondition
	 * @return
	 * @throws SQLException
	 */
	public int retrieveListCountBoardContents(ContentsVO searchCondition){
		int count = 0;
		try {
			count = ((Integer)getSqlMapClientTemplate().queryForObject("Board.retrieveListCountBoardContents", searchCondition)).intValue();
			logger.debug("Board.retrieveListCountBoardContents count:{}", count);
		} catch (Exception e) {
			logger.error("cause:{}", e.toString());   
		} 	
		return count;
	}
	
	/**
	 * retrieveListBoardContents
	 * BoardContents 검색
	 * @param contentsVO
	 * @return
	 * @throws SQLException
	 */
	@SuppressWarnings("unchecked")
	public List<ContentsVO> retrieveListBoardContents(ContentsVO searchCondition){
		
		List<ContentsVO> list = new ArrayList<ContentsVO>();
		try{
			list = list("Board.retrieveListBoardContents", searchCondition);
			logger.debug("Board.retrieveListBoardContents count:{}", list.size());
		}catch(Exception e){
			logger.error("cause:{}", e.toString()); 
		}
		return list;
	}
	
	/**
	 * retrieveListCountBoardContents
	 * BoardContents 검색결과 수
	 * @param searchCondition
	 * @return
	 * @throws SQLException
	 */
	public int getContentsGroupId(){
		int contentsGroupId = 0;
		try {
			contentsGroupId = ((Integer)getSqlMapClientTemplate().queryForObject("Board.getContentsGroupId", null)).intValue();
		} catch (Exception e) {
			logger.error("cause:{}", e.toString());   
		} 	
		return contentsGroupId;
	}
	
	/**
	 * retrieveListCountBoardContents
	 * BoardContents 검색결과 수
	 * @param searchCondition
	 * @return
	 * @throws SQLException
	 */
	public int getContentsSeq(int contentsGroupId){
		int contentsSeq = 0;
		try {
			contentsSeq = ((Integer)getSqlMapClientTemplate().queryForObject("Board.getContentsSeq", contentsGroupId)).intValue();
		} catch (Exception e) {
			logger.error("cause:{}", e.toString());   
		} 	
		return contentsSeq;
	}
	
	
	public BulletinBoardVO retrieveBulletinBoard(BulletinBoardVO bulletinBoard) {
		return (BulletinBoardVO)selectByPk("Board.retrieveBulletinBoard", bulletinBoard);
	}
	
	public void updateViewCnt(ContentsVO searchCondition) {
		update("Board.updateViewCnt", searchCondition);
	}

}